package com.example.im.ui.activity

import android.content.Intent
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.example.im.R
import com.example.im.callback.OnClickUserListener
import com.example.im.bean.UserRoom
import com.example.im.ui.fragment.*
import com.example.im.common.Constants
import com.example.im.manager.ActivityCollector
import com.example.im.mvp.presenter.UserPresenter
import com.example.im.mvp.view.IUserView
import com.example.im.ui.abstracts.AbstractBaseActivity
import kotlinx.android.synthetic.main.include_toolbar.*

class UserActivity : AbstractBaseActivity<UserPresenter>(), IUserView, OnClickUserListener {

    private var mOnToUserSearchFragment: OnToUserSearchFragment? = null

    interface OnToUserSearchFragment {
        fun onSearchFriendGood(userRooms: MutableList<UserRoom>)
    }

    override val layoutId: Int
        get() = R.layout.activity_user

    override fun onAttachFragment(fragment: Fragment) {
        super.onAttachFragment(fragment)
        when (fragment) {
            is UserCentreFragment -> tv_toolbar_title.text = "好友信息"
            is UserSearchFragment -> {
                tv_toolbar_title.text = "好友搜索"
                mOnToUserSearchFragment = fragment
            }
            is UserAddToFragment -> tv_toolbar_title.text = "好友添加"
        }
    }

    override fun initView() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        val serializable = intent.getSerializableExtra(Constants.BUNDLE_T0_USER_ROOM_DATA)
        val transaction = supportFragmentManager.beginTransaction()
        when (intent.getIntExtra(Constants.BUNDLE_T0_USER_ROOM_POINT, 0)) {
            Constants.BUNDLE_T0_USER_POINT_1 -> {
                if (serializable is UserRoom) transaction.replace(
                    R.id.frame_layout, UserCentreFragment.newInstance(serializable)
                )
            }
            Constants.BUNDLE_T0_USER_POINT_2 -> {
                transaction.replace(R.id.frame_layout, UserSearchFragment.newInstance())
            }
            Constants.BUNDLE_T0_USER_POINT_3 -> {
                if (serializable is UserRoom) transaction.replace(
                    R.id.frame_layout, UserAddToFragment.newInstance(serializable)
                )
            }
        }
        transaction.commit()
    }

    override fun initPresenter() {
        mPresenter = UserPresenter(this, this)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> onBackPressed()
        }
        return super.onOptionsItemSelected(item)
    }

    /**
     * 发送聊天-点击事件
     * */
    override fun onClickSendChat(view: View, userRoom: UserRoom) {
        val isFinishing = intent.getBooleanExtra("isFinishing", false)
        if (isFinishing) {
            ActivityCollector.finish(ChatActivity2::class.java)
        }
        val intent = Intent(this, ChatActivity2::class.java)
        intent.putExtra(Constants.BUNDLE_T0_CHAT_DATA, userRoom)
        startActivity(intent)
        finish()
    }

    /**
     * 删除聊天数据-点击事件
     * */
    override fun onClickClearChat(view: View, userRoom: UserRoom) {
        val dialog = AlertDialog.Builder(this)
            .setTitle(userRoom.nickname)
            .setMessage("是否删除该用户聊天记录")
            .setPositiveButton("确定") { _, _ ->
                mPresenter!!.userDeleteChat(userRoom)
            }
            .setNegativeButton("取消", null)
        dialog.show()
    }

    /**
     *删除好友-点击事件
     * */
    override fun onClickDelete(view: View, userRoom: UserRoom) {
        val dialog = AlertDialog.Builder(this)
            .setTitle(userRoom.nickname)
            .setMessage("是否删除该用户")
            .setPositiveButton("确定") { _, _ ->
                mPresenter!!.userDelete(userRoom)
            }
            .setNegativeButton("取消", null)
        dialog.show()
    }

    /**
     * 用户搜索-点击事件
     * */
    override fun onClickSearch(view: View, userId: String) {
        mPresenter!!.userSearch(userId)
    }

    /**
     * 用户条目点击
     * */
    override fun onClickFriendItem(view: View, userRoom: UserRoom) {
        val transaction = supportFragmentManager.beginTransaction()
        if (userRoom.sub == -1) {
            transaction.add(R.id.frame_layout, UserAddToFragment.newInstance(userRoom))
        } else {
            transaction.add(R.id.frame_layout, UserCentreFragment.newInstance(userRoom))
        }
        transaction.addToBackStack(null)
        transaction.commit()
    }

    /**
     * 搜索到的用户结果
     * */
    override fun onSearchFriendGood(userRooms: MutableList<UserRoom>) {
        mOnToUserSearchFragment!!.onSearchFriendGood(userRooms)
    }

    /**
     * 发送添加请求
     * */
    override fun onClickAddTo(view: View, userRoom: UserRoom) {
        mPresenter!!.userAddition(userRoom.jid!!, userRoom.nickname!!)
    }


    override fun onAdditionSuccess() {
        finish()
    }

    /**
     * 删除好友成功调用
     * */
    override fun onRemoverSuccess() {
        startActivity(Intent(this, MainActivity::class.java))
        this.finish()
    }

}